home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 80x0992d.zip / DEBUG.TXT < prev    next >
Text File  |  1992-09-30  |  14KB  |  334 lines

  1.                             Anti Debugging Tricks
  2.  
  3.                                      By:
  4.  
  5.                                   Inbar Raz
  6.  
  7.                                Release number 2
  8.  
  9.   Today's anti debugging tricks devide into two categories:
  10.  
  11.   1. Preventive actions;
  12.   2. Self-modifying code.
  13.  
  14.   Most debugging tricks, as for today, are used within viruses, in order to
  15. avoid dis-assembly of the virus, as it will be exampled later in this file.
  16. Another big part of anti debugging tricks is found with software protection
  17. programs, what use them in order to make the cracking of the protection
  18. harder.
  19.  
  20. 1. Preventive actions:
  21. ----------------------
  22.  
  23.   Preventive actions are, basically, actions that the program takes in order
  24. to make the user unable to dis-assemble the code or trace it while running.
  25.  
  26. 1.1. Interrupt disable:
  27.  
  28.        Interrupt disable is probably the most common form of anti-debugging
  29.      trick. It can be done in several ways:
  30.  
  31.    1.1.1. Hardware masking of interrupt:
  32.  
  33.             In order to avoid tracing of a code, one usually disables the
  34.           interrupt via the 8259 Interrupt Controller, addressed by read/write
  35.           actions to port 21h. The 8259 Interrupt Controller controls the IRQ
  36.           lines. This means that any IRQ between 0 and 7 may be disabled by
  37.           this action. Bit 0 is IRQ0, bit 1 is IRQ1 etc. Since IRQ1 is the
  38.           keyboard interrupt, you may disable the keyboard without the
  39.           debugger being able to bypass it.
  40.  
  41.           Example:
  42.  
  43.           CS:0100 E421           IN     AL,21
  44.           CS:0102 0C02           OR     AL,02
  45.           CS:0104 E621           OUT    21,AL
  46.  
  47.             Just as a side notice, the keyboard may be also disabled by
  48.           commanding the Programmable Perepheral Interface (PPI), port 61h.
  49.  
  50.           Example:
  51.  
  52.           CS:0100 E461           IN     AL,61
  53.           CS:0102 0C80           OR     AL,80
  54.           CS:0104 E661           OUT    61,AL
  55.  
  56.    1.1.2. Software masking of interrupt:
  57.  
  58.             This is quite an easy form of anti-debugging trick. All you have
  59.           to do is simply replace the vectors of interrupts debuggers use/any
  60.           other interrupt you will not be using or expecting to happen. Do not
  61.           forget to restore the original vectors when you are finished.
  62.           It is adviseable to use manual change of vector, as shown below,
  63.           rather than to change it using interrupt 21h service 25h, because
  64.           any debugger that has gained control of interrupt 21h may replace
  65.           your vector with the debugger's. The example shows an interception
  66.           of interrupt 03h - the breakpoint interrupt.
  67.  
  68.           Example:
  69.  
  70.           CS:0100 EB04           JMP    0106
  71.           CS:0102 0000           ADD    [BX+SI],AL
  72.           CS:0104 0000           ADD    [BX+SI],AL
  73.           CS:0106 31C0           XOR    AX,AX
  74.           CS:0108 8EC0           MOV    ES,AX
  75.           CS:010A 268B1E0C00     MOV    BX,ES:[000C]
  76.           CS:010F 891E0201       MOV    [0102],BX
  77.           CS:0113 268B1E0E00     MOV    BX,ES:[000E]
  78.           CS:0118 891E0401       MOV    [0104],BX
  79.           CS:011C 26C7064C000000 MOV    Word Ptr ES:[000C],0000
  80.           CS:0123 26C7064E000000 MOV    Word Ptr ES:[000E],0000
  81.  
  82.    1.1.3. Vector manipulation
  83.  
  84.             This method involves manipulations of the interrupt vectors,
  85.           mainly for proper activation of the algorithm. Such action, as
  86.           exampled, may be used to decrypt a code (see also 2.1), using data
  87.           stored ON the vectors. Ofcourse, during normal operation of the
  88.           program, vectors 01h and 03h are not used, so unless you are trying
  89.           to debug such a program, it works fine.
  90.  
  91.           Example:
  92.  
  93.           CS:0100 31C0           XOR    AX,AX
  94.           CS:0102 8ED0           MOV    SS,AX
  95.           CS:0104 BC0600         MOV    SP,0006
  96.           CS:0107 8B0E0211       MOV    CX,[1102]
  97.           CS:010B 50             PUSH   AX
  98.           CS:010C 21C8           AND    AX,CX
  99.           CS:010E 01C5           ADD    BP,AX
  100.           CS:0110 58             POP    AX
  101.           CS:0111 E2F8           LOOP   010B
  102.  
  103.    1.1.4. Interrupt replacement
  104.  
  105.             This is a really nasty trick, and it should be used ONLY if you
  106.           are ABSOLUTELY sure that your programs needs no more debugging. What
  107.           it does is simply copy the vectors of some interrupts you will be
  108.           using, say 16h and 21h, onto the vectors of interrupt 01h and 03h,
  109.           that do not occure during normal operation of the program. If the
  110.           user wants to debug the program, he would have to search for every
  111.           occurance of INT 01, and replace it with the appropriate INT
  112.           instruction.
  113.  
  114.           Example:
  115.  
  116.           CS:0100 FA             CLI
  117.           CS:0101 31C0           XOR    AX,AX
  118.           CS:0103 8EC0           MOV    ES,AX
  119.           CS:0105 26A18400       MOV    AX,ES:[0084]
  120.           CS:0109 26A30400       MOV    ES:[0004],AX
  121.           CS:010D 26A18600       MOV    AX,ES:[0086]
  122.           CS:0111 26A30600       MOV    ES:[0006],AX
  123.           CS:0115 B44C           MOV    AH,4C
  124.           CS:0117 CD01           INT    01
  125.  
  126. 1.2. Time watch:
  127.  
  128.        This may be a less common method, but it is usefull against debuggers
  129.      that disable all interrupts except for the time that the program is
  130.      executed, such as Borland's Turbo Debugger. This method simply retains
  131.      the value of the clock counter, updated by interrupt 08h, and waits in an
  132.      infinite loop until the value changes. Another example is when you mask
  133.      the timer interrupt by ORing the value INed from port 21h with 01h and
  134.      then OUTing it back, thus disabling the IRQ0 - Timer interrupt. Note that
  135.      this method is usefull only against RUN actions, not TRACE/PROCEED ones.
  136.  
  137.      Example:
  138.  
  139.      CS:0100 2BC0           SUB    AX,AX
  140.      CS:0102 FB             STI
  141.      CS:0103 8ED8           MOV    DS,AX
  142.      CS:0105 8A266C04       MOV    AH,[046C]
  143.      CS:0109 A06C04         MOV    AL,[046C]
  144.      CS:010C 3AC4           CMP    AL,AH
  145.      CS:010E 74F9           JZ     0109
  146.  
  147. 1.3. Fool the debugger:
  148.  
  149.        This is a very nice technique, that works especially and only on those
  150.      who use Turbo Debugger or its kind. What you do is init a jump to a
  151.      middle of an instruction, whereas the real address actually contains
  152.      another opcode. If you work with a normal step debugger such as Debug or
  153.      SymDeb, it won't work since the debugger jumps to the exact address of
  154.      the jump, and not to the beginning of an instruction at the closest
  155.      address, like Turbo Debugger.
  156.  
  157.      Example:
  158.  
  159.      CS:0100 E421           IN     AL,21
  160.      CS:0102 B0FF           MOV    AL,FF
  161.      CS:0104 EB02           JMP    0108
  162.      CS:0106 C606E62100     MOV    Byte Ptr [21E6],00
  163.      CS:010B CD20           INT    20
  164.  
  165.      Watch this:
  166.  
  167.      CS:0108 E621           OUT    21,AL
  168.  
  169. 1.4. Cause debugger to stop execution:
  170.  
  171.        This is a technique that causes a debugger to stop the execution of a
  172.      certain program. What you need to do is to put some INT 3 instructions
  173.      over the code, at random places, and any debugger trying to run will stop
  174.      there. Since this techniqu causes the CPU to stop executing the program,
  175.      and therefore clear the Prefetch Instruction Queue, it is adviseable to
  176.      use this techinque in conjunction with the PIQ trick, 2.2.2. Note that
  177.      the example shows how to use these two tricks together.
  178.  
  179.      Example:
  180.  
  181.      CS:0100 B97502         MOV    CX,0275
  182.      CS:0103 BE9001         MOV    SI,0190
  183.      CS:0106 89F7           MOV    DI,SI
  184.      CS:0108 AC             LODSB
  185.      CS:0109 C70610013473   MOV    Word Ptr [0110],7334
  186.      CS:010F CC             INT    3
  187.      CS:0110 2406           AND    AL,06
  188.      CS:0112 AA             STOSB
  189.      CS:0113 C70610012406   MOV    Word Ptr [0110],0624
  190.      CS:0119 E2ED           LOOP   0108
  191.  
  192. 1.5. Halt TD386 V8086 mode:
  193.  
  194.        This is a nice way to fool Turbo Debugger's V8086 module (TD386). It is
  195.      baed on the fact that TD386 does not use INT 00h to detect division by
  196.      zero (or register overrun after division, which is treated by the
  197.      processor in the same way as in case of division by zero). When TD386
  198.      detects a division fault it aborts, reporting about the faulty
  199.      division. In real mode (even under a regular debugger), a faulty DIV
  200.      instruction will cause INT 00h to be called. Therefore, pointing INT 00h
  201.      to the next instruction, will recover from the faulty DIV.
  202.  
  203.      Note: It is very important to restore INT 00h's vector. Otherwise, the
  204.      next call to INT 00h will cause the machine to hang.
  205.  
  206.      Example:
  207.  
  208.      CS:0100 31C0          XOR     AX,AX
  209.      CS:0102 8ED8          MOV     DS,AX
  210.      CS:0104 C70600001201  MOV     WORD PTR [0000],0112
  211.      CS:010A 8C0E0200      MOV     [0002],CS
  212.      CS:010E B400          MOV     AH,00
  213.      CS:0110 F6F4          DIV     AH
  214.      CS:0112 B8004C        MOV     AX,4C00
  215.      CS:0115 CD21          INT     21
  216.  
  217. 1.6. Halt any V8086 process:
  218.  
  219.        Another way of messing TD386 is fooling it into an exception.
  220.      Unfortunately, this exception will also be generated under any other
  221.      program, running at V8086 mode. The exception is exception #13, and its
  222.      issued interrupt is INT 0Dh - 13d. The idea is very similar to the
  223.      divide by zero trick: Causing an exception, when the exception interrupt
  224.      points to somewhere in the program's code. It will always work when the
  225.      machine is running in real mode, but never under the V8086 mode.
  226.  
  227.      Note: It is very important to restore the original interrupt vectors.
  228.      Otherwise, the next exception will hang the machine.
  229.  
  230.      Example:
  231.  
  232.      CS:0100 31C0          XOR     AX,AX
  233.      CS:0102 8ED8          MOV     DS,AX
  234.      CS:0104 C70634001301  MOV     WORD PTR [0034],0113
  235.      CS:010A 8C0E3600      MOV     [0036],CS
  236.      CS:010E 833EFFFF00    CMP     WORD PTR [FFFF],+00
  237.      CS:0113 B8004C        MOV     AX,4C00
  238.      CS:0116 CD21          INT     21
  239.  
  240. 2. Self-modifying code:
  241. -----------------------
  242.  
  243. 2.1. Encryptive/decryptive algorithm:
  244.  
  245.        The first category is simply a code, that has been encrypted, and has
  246.      been added with a decryption routine. The trick here is that when a
  247.      debugger sets up a breakpoint, it simply places the opcode CCh (INT 03h)
  248.      in the desired address, and once that interrupt is executed, the debugger
  249.      regains control of things. If you try to set a breakpoint AFTER the
  250.      decryption algorithm, what is usually needed, you will end up putting an
  251.      opcode CCh in a place where decryption action is taken, therefore losing
  252.      your original CCh in favour of whatever the decryption algorithm makes.
  253.      The following example was extracted from the Haifa virus. If you try to
  254.      set a breakpoint at address CS:0110, you will never reach that address,
  255.      since there is no way to know what will result from the change. Note that
  256.      if you want to make the tracing even harder, you should start the
  257.      decryption of the code from its END, so it takes the whole operation
  258.      until the opcode following the decryption routine is decrypted.
  259.  
  260.      Example:
  261.  
  262.      CS:0100 BB7109         MOV    BX,0971
  263.      CS:0103 BE1001         MOV    DI,0110
  264.      CS:0106 91             XCHG   AX,CX
  265.      CS:0107 91             XCHG   AX,CX
  266.      CS:0108 2E803597       XOR    Byte Ptr CS:[DI],97
  267.      CS:010C 47             INC    DI
  268.      CS:010D 4B             DEC    BX
  269.      CS:010E 75F6           JNZ    0106
  270.      CS:0110 07             POP    ES
  271.      CS:0111 07             POP    ES
  272.  
  273. 2.2. Self-modifying code:
  274.  
  275.    2.2.1. Simple self-modification:
  276.  
  277.             This method implements the same principle as the encryption
  278.           method: Change the opcode before using it. In the following example,
  279.           we change the insruction following the call, and therefore, if you
  280.           try to trace the entire call ('P'/Debug or F8/Turbo Debugger), you
  281.           will not succeed, since the debugger will put its CCh on offset 104h,
  282.           but when the routine runs, it overwrites location 104h.
  283.  
  284.           Example:
  285.  
  286.           CS:0100 E80400         CALL   0107
  287.           CS:0103 CD20           INT    20
  288.           CS:0105 CD21           INT    21
  289.           CS:0107 C7060301B44C   MOV    Word Ptr [0103],4CB4
  290.           CS:010D C3             RET
  291.  
  292.           Watch this:
  293.  
  294.           CS:0103 B44C           MOV    AH,4C
  295.  
  296.    2.2.2. Prefetch Instruction Queue (PIQ) manipulation:
  297.  
  298.             This method is a bit similar to (1.3), but it fools ANY debugger,
  299.           or any other process that executes one operation at a time. The PIQ
  300.           is an area within the CPU, that pre-fethces, ie. takes in advance,
  301.           instructions from memory, so when they need to be executed, it
  302.           would take less time to get them, since they are already in the CPU.
  303.           The PIQ length ranges from 6 or 4 in old computers, up to as high as
  304.           25 in new ones. What the trick does is change the FOLLOWING opcode
  305.           to something meaningless. If you are debugging, then the change will
  306.           take place BEFORE the instructions is executed or fetched. If you
  307.           run the program NORMALLY, by the time you change the opcode, it will
  308.           have already been fetched.
  309.  
  310.           Example:
  311.  
  312.           CS:0100 B97502         MOV    CX,0275
  313.           CS:0103 BE9001         MOV    SI,0190
  314.           CS:0106 89F7           MOV    DI,SI
  315.           CS:0108 AC             LODSB
  316.           CS:0109 C7060F012406   MOV    Word Ptr [010F],0624
  317.           CS:010F 3473           XOR    AL,73
  318.           CS:0111 AA             STOSB
  319.           CS:0112 C7060F012406   MOV    Word Ptr [010F],0624
  320.           CS:0118 E2EE           LOOP   0108
  321.  
  322.           Watch this:
  323.  
  324.           CS:010F 2406           AND    AL,06
  325.  
  326. ===============================================================================
  327.  
  328. Thanks to Yogo and Ford Prefect for helping me assemble this list.
  329.  
  330. Any new ideas and updates should be sent to: Inbar Raz, 2:401/100.1  or
  331. 2:403/123.42.
  332.  
  333. Inbar Raz
  334.